<!DOCTYPE html>
<html>
<head>
<script src="../../../resources/js-test.js"></script>
</head>
<body>
<script>

description("This tests the constructor for the MessageEvent DOM class.");

var test_object = {nyannyan: 123};

// No initializer is passed.
shouldBe("new MessageEvent('eventType').bubbles", "false");
shouldBe("new MessageEvent('eventType').cancelable", "false");
shouldBe("new MessageEvent('eventType').data", "null");
shouldBeEqualToString("new MessageEvent('eventType').origin", "");
shouldBeEqualToString("new MessageEvent('eventType').lastEventId", "");
shouldBe("new MessageEvent('eventType').source", "null");
shouldBe("new MessageEvent('eventType').ports", "[]");

// bubbles is passed.
shouldBe("new MessageEvent('eventType', { bubbles: false }).bubbles", "false");
shouldBe("new MessageEvent('eventType', { bubbles: true }).bubbles", "true");

// cancelable is passed.
shouldBe("new MessageEvent('eventType', { cancelable: false }).cancelable", "false");
shouldBe("new MessageEvent('eventType', { cancelable: true }).cancelable", "true");

// data is passed.
shouldBe("new MessageEvent('eventType', { data: test_object }).data", "test_object");
shouldBe("new MessageEvent('eventType', { data: document }).data", "document");
shouldBe("new MessageEvent('eventType', { data: undefined }).data", "null");
shouldBe("new MessageEvent('eventType', { data: null }).data", "null");
shouldBe("new MessageEvent('eventType', { data: false }).data", "false");
shouldBe("new MessageEvent('eventType', { data: true }).data", "true");
shouldBeEqualToString("new MessageEvent('eventType', { data: '' }).data", "");
shouldBeEqualToString("new MessageEvent('eventType', { data: 'chocolate' }).data", "chocolate");
shouldBe("new MessageEvent('eventType', { data: 12345 }).data", "12345");
shouldBe("new MessageEvent('eventType', { data: 18446744073709551615 }).data", "18446744073709552000");
shouldBe("new MessageEvent('eventType', { data: NaN }).data", "NaN");
// Note that valueOf() is not called, when the left hand side is evaluated.
shouldBeFalse("new MessageEvent('eventType', { data: {valueOf: function () { return test_object; } } }).data == test_object");
shouldBe("new MessageEvent('eventType', { get data() { return 123; } }).data", "123");
shouldThrow("new MessageEvent('eventType', { get data() { throw 'MessageEvent Error'; } })");

// origin or lastEventId is passed.
["origin", "lastEventId"].forEach(function (attr) {
    // Strings.
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": 'melancholy' })." + attr, "melancholy");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": '' })." + attr, "");

    // Non-strings.
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": undefined })." + attr, "");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": null })." + attr, "null");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": false })." + attr, "false");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": true })." + attr, "true");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": 12345 })." + attr, "12345");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": 18446744073709551615 })." + attr, "18446744073709552000");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": NaN })." + attr, "NaN");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": [] })." + attr, "");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": [1, 2, 3] })." + attr, "1,2,3");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": {melancholy: 12345} })." + attr, "[object Object]");
    shouldBeEqualToString("new MessageEvent('eventType', { " + attr + ": {valueOf: function () { return 'melancholy'; } } })." + attr, "[object Object]");
    shouldBeEqualToString("new MessageEvent('eventType', { get " + attr + "() { return 123; } })." + attr, "123");
    shouldThrow("new MessageEvent('eventType', { get " + attr + "() { throw 'MessageEvent Error'; } })");
});

// source is passed.
// Window objects.
shouldBe("new MessageEvent('eventType', { source: window }).source", "window");
shouldBe("new MessageEvent('eventType', { source: this }).source", "this");

// MessagePort objects.
var channel = new MessageChannel();
shouldBe("new MessageEvent('eventType', { ports: [channel.port1], source: channel.port1 }).source", "channel.port1");

// Unacceptable source objects (not a Window or a MessagePort).
shouldThrow("new MessageEvent('eventType', { source: test_object }).source");
shouldThrow("new MessageEvent('eventType', { source: document }).source");
shouldThrow("new MessageEvent('eventType', { source: document.body }).source");
shouldBe("new MessageEvent('eventType', { source: undefined }).source", "null");
shouldBe("new MessageEvent('eventType', { source: null }).source", "null");
shouldThrow("new MessageEvent('eventType', { source: false }).source");
shouldThrow("new MessageEvent('eventType', { source: true }).source");
shouldThrow("new MessageEvent('eventType', { source: '' }).source");
shouldThrow("new MessageEvent('eventType', { source: 'chocolate' }).source");
shouldThrow("new MessageEvent('eventType', { source: 12345 }).source");
shouldThrow("new MessageEvent('eventType', { source: 18446744073709551615 }).source");
shouldThrow("new MessageEvent('eventType', { source: NaN }).source");
// Note that valueOf() is not called, when the left hand side is evaluated.
shouldThrow("new MessageEvent('eventType', { source: {valueOf: function () { return window; } } }).source == window");
shouldBe("new MessageEvent('eventType', { get source() { return window; } }).source", "window");
shouldThrow("new MessageEvent('eventType', { get source() { return 123; } }).source");
shouldThrow("new MessageEvent('eventType', { get source() { throw 'MessageEvent Error'; } })");

// ports is passed.
// Valid message ports.
var channel2 = new MessageChannel();
shouldBe("new MessageEvent('eventType', { ports: [channel.port1, channel.port2, channel2.port1] }).ports[0]", "channel.port1");
shouldBe("new MessageEvent('eventType', { ports: [channel.port1, channel.port2, channel2.port1] }).ports[1]", "channel.port2");
shouldBe("new MessageEvent('eventType', { ports: [channel.port1, channel.port2, channel2.port1] }).ports[2]", "channel2.port1");
shouldBe("new MessageEvent('eventType', { ports: [] }).ports", "[]");
shouldBe("new MessageEvent('eventType', { ports: undefined }).ports", "[]");

// Invalid message ports.
shouldThrow("new MessageEvent('eventType', { ports: [1, 2, 3] }).ports[2]");
shouldThrow("new MessageEvent('eventType', { ports: test_object }).ports");
shouldThrow("new MessageEvent('eventType', { ports: document }).ports");
shouldThrow("new MessageEvent('eventType', { ports: false }).ports");
shouldThrow("new MessageEvent('eventType', { ports: true }).ports");
shouldThrow("new MessageEvent('eventType', { ports: '' }).ports");
shouldThrow("new MessageEvent('eventType', { ports: 'chocolate' }).ports");
shouldThrow("new MessageEvent('eventType', { ports: 12345 }).ports");
shouldThrow("new MessageEvent('eventType', { ports: 18446744073709551615 }).ports");
shouldThrow("new MessageEvent('eventType', { ports: NaN }).ports");
shouldThrow("new MessageEvent('eventType', { get ports() { return 123; } }).ports");
shouldThrow("new MessageEvent('eventType', { get ports() { throw 'MessageEvent Error'; } })");
shouldThrow("new MessageEvent('eventType', { ports: null }).ports");
// Note that valueOf() is not called, when the left hand side is evaluated.
shouldThrow("new MessageEvent('eventType', { ports: {valueOf: function () { return [channel.port1, channel.port2, channel.port2]; } } }).ports[0]");
shouldThrow("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: {length: 3, 0: channel.port1, 1: channel.port2, 2: channel2.port1} }).ports[2]");

// All initializers are passed.
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).bubbles", "true");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).cancelable", "true");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).data", "test_object");
shouldBeEqualToString("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).origin", "wonderful");
shouldBeEqualToString("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).lastEventId", "excellent");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).source", "window");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).ports[0]", "channel.port1");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).ports[1]", "channel.port2");
shouldBe("new MessageEvent('eventType', { bubbles: true, cancelable: true, data: test_object, origin: 'wonderful', lastEventId: 'excellent', source: window, ports: [channel.port1, channel.port2, channel2.port1] }).ports[2]", "channel2.port1");
</script>
</body>
</html>
